package com.franklin.java.action.sql.entity.ui;

import com.flychord.jdk.util.matchcase.TupleCase;
import com.flychord.jdk.util.tuple.Tuple2;
import com.franklin.plugins.common.collection.PrettyArrayList;
import com.franklin.plugins.common.utils.DataCheckUtils;
import com.intellij.ui.tree.BaseTreeModel;

import java.util.List;
import java.util.Set;

/**
 * @author Franklin
 * @since 2021/9/13 10:30
 */
public class ResultMapTreeModel extends BaseTreeModel<ResultMapChildNode> {

    private ResultMapNode resultMapNode;

    public ResultMapTreeModel(ResultMapNode resultMapNode) {
        this.resultMapNode = resultMapNode;
    }

    @Override
    public List<? extends ResultMapChildNode> getChildren(Object parent) {
        ResultMapChildNode resultMapChildNode = (ResultMapChildNode) parent;
        List<Tuple2<String, String>> properties = resultMapChildNode.getProperties();
        List<ResultMapChildNode> result = new PrettyArrayList<>();
        properties.forEach(
            TupleCase.match(
                (key,value) -> {
                    if (DataCheckUtils.isNotEmpty(key) && DataCheckUtils.isNotEmpty(value)) {
                        result.add(new PropertyNode(resultMapChildNode,key,value));
                    }
                }
            )
        );

        List<ResultMapChildNode> childrenNodeList = resultMapChildNode.getChildrenNodeList();
        if (DataCheckUtils.isNotEmpty(childrenNodeList)) {
            result.addAll(childrenNodeList);
        }

        return result;
    }

    @Override
    public Object getRoot() {
        return resultMapNode;
    }


}
